package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysVehicle;
import com.ruoyi.system.service.ISysVehicleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 车辆管理Controller
 * 
 * @author ruoyi
 * @date 2024-03-11
 */
@RestController
@RequestMapping("/system/vehicle")
public class SysVehicleController extends BaseController
{
    @Autowired
    private ISysVehicleService sysVehicleService;

    /**
     * 查询车辆管理列表
     */
    //@PreAuthorize("@ss.hasPermi('system:vehicle:list')")
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(SysVehicle sysVehicle)
    {
        startPage();
        List<SysVehicle> list = sysVehicleService.selectSysVehicleList(sysVehicle);
        return getDataTable(list);
    }

    /**
     * 导出车辆管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:vehicle:export')")
    @Log(title = "车辆管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysVehicle sysVehicle)
    {
        List<SysVehicle> list = sysVehicleService.selectSysVehicleList(sysVehicle);
        ExcelUtil<SysVehicle> util = new ExcelUtil<SysVehicle>(SysVehicle.class);
        util.exportExcel(response, list, "车辆管理数据");
    }

    /**
     * 获取车辆管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:vehicle:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysVehicleService.selectSysVehicleById(id));
    }

    /**
     * 新增车辆管理
     */
    @PreAuthorize("@ss.hasPermi('system:vehicle:add')")
    @Log(title = "车辆管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysVehicle sysVehicle)
    {

        sysVehicle.setCreateBy(getUsername());
        return toAjax(sysVehicleService.insertSysVehicle(sysVehicle));
    }

    /**
     * 修改车辆管理
     */
    @PreAuthorize("@ss.hasPermi('system:vehicle:edit')")
    @Log(title = "车辆管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysVehicle sysVehicle)
    {
        sysVehicle.setUpdateBy(getUsername());
        return toAjax(sysVehicleService.updateSysVehicle(sysVehicle));
    }

    /**
     * 删除车辆管理
     */
    @PreAuthorize("@ss.hasPermi('system:vehicle:remove')")
    @Log(title = "车辆管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysVehicleService.deleteSysVehicleByIds(ids));
    }

    /**
     * 车辆图片上传
     */
    @Log(title = "车辆图片", businessType = BusinessType.UPDATE)
    @PostMapping("/upload/file")
    public AjaxResult avatar(@RequestParam("vehiclefile") MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            String vehicle = FileUploadUtils.upload(RuoYiConfig.getVehiclePath(), file, MimeTypeUtils.IMAGE_EXTENSION);

                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", vehicle);
                return ajax;

        }
        return error("上传图片异常，请联系管理员");
    }
}
